package com.drought.rained.auth;

import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.drought.rained.R;
import com.drought.rained.base.BaseTitleActivity;
import com.drought.rained.bean.AuthInfoDataLJ;
import com.drought.rained.bean.DictionaryItemDataLJ;
import com.drought.rained.business.AuthRouterLJ;
import com.drought.rained.business.RouterMapLJ;
import com.drought.rained.cache.UserInfoLJ;
import com.drought.rained.common.ConstantLJ;
import com.drought.rained.mvpview.DictionaryMvpViewLJ;
import com.drought.rained.mvpview.MoneyAuthMvpViewLJ;
import com.drought.rained.presenter.DictionaryPresenterLJ;
import com.drought.rained.presenter.MoneyAuthPresenterLJ;
import com.drought.rained.response.AuthInfoResponseLJ;
import com.drought.rained.response.BlankResponseLJ;
import com.drought.rained.utils.ToastUtilsLJ;
import com.drought.rained.view.MenuDialogLJ;

import java.util.ArrayList;
import java.util.Arrays;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

@Route(name = "借款金额认证", path = RouterMapLJ.MONEY_AUTH_PAGE)
public class MoneyAuthActivityLJ extends BaseTitleActivity implements MoneyAuthMvpViewLJ, DictionaryMvpViewLJ {

    private final String DICTIONARY_TYPE_NEED_MONEY = "need_money"; // 需要多少钱
    private final String DICTIONARY_TYPE_USAGE_OF_LOAN = "usage_of_loan"; // 借款用途

    @BindView(R.id.et_need_money)
    EditText etNeedMoney;
    @BindView(R.id.et_usage)
    EditText etUsage;
    @BindView(R.id.tv_next_step)
    TextView tvNextStep;

    @Autowired(name = ConstantLJ.IS_BACK)
    boolean is_back;
    private ArrayList<DictionaryItemDataLJ> mNeedMoneyList;
    private ArrayList<DictionaryItemDataLJ> mUsageList;
    private MoneyAuthPresenterLJ moneyAuthPresenter;
    private DictionaryPresenterLJ dictionaryPresenter;
    private MenuDialogLJ mMenuDialog;

    @Override
    protected void initViews() {
        ButterKnife.bind(this);
    }

    @Override
    protected void onUIReady() {
        ARouter.getInstance().inject(this);

        moneyAuthPresenter = new MoneyAuthPresenterLJ(this);
        dictionaryPresenter = new DictionaryPresenterLJ(this);
        String[] dicts = {DICTIONARY_TYPE_NEED_MONEY, DICTIONARY_TYPE_USAGE_OF_LOAN};
        dictionaryPresenter.getDictionaryData(Arrays.asList(dicts));
        if (UserInfoLJ.isShowUserInformation())
            moneyAuthPresenter.getAuthInfo();
        if (is_back) {
            tvNextStep.setText(R.string.save_change);
        }
    }

    @Override
    protected int getContentResId() {
        return R.layout.activity_money_auth_lj;
    }

    @Override
    public String getPageTitle() {
        return getStringResource(R.string.money_auth_page_name);
    }

    @Override
    public void onGetDataNext(AuthInfoResponseLJ response) {
        fillTextView(response.getData().getBorrowingNeeds());
    }

    @Override
    public void onGetDataNext(BlankResponseLJ response) {
        dismissLoadingView();
        if (response.getStatus() == ConstantLJ.HTTP_OK) {
            AuthRouterLJ.getInstance().jumpNextPager(this, RouterMapLJ.MONEY_AUTH_PAGE);
        } else {
            ToastUtilsLJ.showShort(this, response.getMessage());
        }
    }

    @Override
    public void onGetDataError(Throwable throwable) {
        dismissLoadingView();
    }

    @Override
    public void onGetDataNext(ArrayList<ArrayList<DictionaryItemDataLJ>> responseWrapper) {
        if (responseWrapper.size() >= 2) {
            mNeedMoneyList = responseWrapper.get(0);
            mUsageList = responseWrapper.get(1);
        }

    }

    public void fillTextView(AuthInfoDataLJ.BorrowingNeedsBean borrowingNeedsBean) {
        etNeedMoney.setText(borrowingNeedsBean.getNeedMoneyValue());
        etNeedMoney.setTag(borrowingNeedsBean.getNeedMoney());
        etUsage.setText(borrowingNeedsBean.getMoneyToDoValue());
        etUsage.setTag(borrowingNeedsBean.getMoneyTodo());

    }


    public MenuDialogLJ getMenuDialog() {
        if (mMenuDialog == null) {
            mMenuDialog = new MenuDialogLJ(this, R.style.dialog_bottom_full);
        }
        return mMenuDialog;
    }

    private void picker_need_money() {
        if (mNeedMoneyList == null || mNeedMoneyList.size() == 0) {
            return;
        }
        getMenuDialog().
                showDialog(etNeedMoney, mNeedMoneyList);
    }

    private void picker_usage() {
        if (mUsageList == null || mUsageList.size() == 0) {
            return;
        }
        getMenuDialog().
                showDialog(etUsage, mUsageList);
    }

    @OnClick({R.id.rl_need_money, R.id.et_need_money, R.id.rl_usage, R.id.et_usage, R.id.tv_next_step})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.rl_need_money:
            case R.id.et_need_money:
                picker_need_money();
                break;
            case R.id.rl_usage:
            case R.id.et_usage:
                picker_usage();
                break;
            case R.id.tv_next_step:
                commit();
                break;
        }

    }

    private void commit() {
        String authPageCode = AuthRouterLJ.getInstance().routerMap2Code(RouterMapLJ.MONEY_AUTH_PAGE);
        String mNeedMoney = (String) etNeedMoney.getTag();
        String mUsage = (String) etUsage.getTag();
        if (TextUtils.isEmpty(mNeedMoney)) {
            ToastUtilsLJ.showShort(this, R.string.money_auth_need_money_hint);
            return;
        } else if (TextUtils.isEmpty(mUsage)) {
            ToastUtilsLJ.showShort(this, R.string.money_auth_usage_hint);
            return;
        }

        moneyAuthPresenter.saveAuthInfo(mNeedMoney, mUsage, authPageCode);
        showLoadingView();

    }

}
